package com.lmm.rpc.server.autoconfig;

import com.lmm.rpc.core.config.ZKServerConfig;
import com.lmm.rpc.core.register.Registry;
import com.lmm.rpc.core.register.ZkRegister;
import com.lmm.rpc.core.server.NettyServer;
import com.lmm.rpc.core.server.Server;
import com.lmm.rpc.server.processor.RpcServerProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;

/**
 * @title: RpcServerAutoConfig
 * @Description: 自动配置
 * @Author Lmm
 * @Date: 2022/10/9 11:17
 * @Version 1.0
 */
@Configuration
@EnableConfigurationProperties({RpcServerProperties.class})
public class RpcServerAutoConfig {
    @Autowired
    private RpcServerProperties properties;

    /**
     * 注册中心的bean
     * */
    @Bean(name = "registry")
    @ConditionalOnMissingBean
    public Registry registry(){
        return new ZkRegister(new ZKServerConfig(properties.getZkPath(),null,properties.getEnv(),properties.getPort(),null));
    }

    @Bean(name = "server")
    @ConditionalOnMissingBean
    public Server server(){
        return new NettyServer(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()),null, properties.getBossThreadCount(), properties.getWorkThreadCount());
    }
    @Bean
    @ConditionalOnMissingBean
    public RpcServerProcessor rpcServerProcessor(@Autowired Registry registry,
                                                 @Autowired Server server,
                                                 @Autowired RpcServerProperties properties){
        return new RpcServerProcessor(registry,properties,server);
    }
}
